package com.wb.iec104.analysis;


import com.wb.tcp.common.utils.ConstUtils;
import com.wb.iec104.utils.Util;
import lombok.extern.slf4j.Slf4j;

/**
 * 104解析
 *
 * @author Lenovo
 */
@Slf4j
public class AnalysisIEC104   {

    public  void analysis( String message) throws Exception {


        String mes = message.replaceAll(" ", "");
        if ((mes.length() == 0) || (mes.length() % 2) == 1) {
            //104 报文没有结束字符，所以最好判断一下报文串的长度是否是偶数
            return ;
        }
        // 将报文转化成int数组
        int msgArray[] = Util.hexStringToIntArray(mes);
        // 记录报文的长度
        int length = msgArray.length;
        if (msgArray[0] != 0x68 || length < 2) {
            return ;
        }
        if (length != msgArray[1] + 2) {
            return ;
        }

        if ((msgArray[2] & 0x03) != 3 && (msgArray[2] & 0x03) != 1) {
            //解析ASDU
            int asdu[] = new int[length - 6];
            for (int j = 0; j < length - 6; j++) {
                asdu[j] = msgArray[6 + j];
            }
            ASDU.ASDU_analysis(asdu);
        }

    }

}
